from common.log_info import Logger
from common.config import *
import requests

# appid = "wxe5315e55aeda9e62"
# identifierType = "OFFICIAL_ACCOUNTS"
# registerChannelType = "OFFICIAL_ACCOUNTS"

member = Logger("member", "member").logger


class Member:

    # 会员列表查询
    def member_list(self, env, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/member/page"
        data = {"current": 1, "size": 20}
        member.info(f"会员列表查询：{data}")
        return requests.post(url, headers=header, json=data)

    # 修改积分
    def change_point(self, env, data, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-point-server/v1/point/changePoint"
        member.info(f"修改积分的请求参数：{data}")
        return requests.post(url, headers=header, json=data)

    # 查询积分
    def get_total_point(self, env, member_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/member/welfare/statistics/{}".format(member_id)
        member.info(f"查询积分的请求参数：{member_id}")
        return requests.get(url, headers=header)

    # 查询配置列表
    def get_member_config(self, env, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/memberFieldConfig"
        return requests.get(url, headers=header)

    # 添加会员配置
    def add_member_config(self, env, data, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/memberFieldConfig/"
        member.info(f"添加会员配置请求参数：{data}")
        return requests.post(url, headers=header, json=data)

    # 编辑会员配置
    def edit_member_config(self, env, field_id, data, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"platform-member-server/v1/memberFieldConfig/{field_id}"
        member.info(f"编辑会员配置的id：{field_id}和请求参数：{data}")
        return requests.patch(url, headers=header, json=data)

    # 删除会员配置
    def delete_member_config(self, env, field_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"platform-member-server/v1/memberFieldConfig/{field_id}"
        member.info(f"删除会员配置的请求参数：{field_id}")
        return requests.delete(url, headers=header)

    # 添加会员等级
    def member_level(self, env, header, level_id=None):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/member/level"
        if not level_id:
            data = {
                "effectiveType": "PERMANENT",
                "growth": 999998,
                "levelIcon": "https://buorgcostest.rongyi.com/member-logo/258/156804920652208771845907650977469920977.jpeg",
                "levelName": "自动化测试专用会员等级-独秀",
                "thirdLevelConnectKey": "1"
            }
            member.info(f"添加等级的请求参数：{data}")
            return requests.post(url, headers=header, json=data)
        else:
            data = {
                "levelName": "编辑自动化测试专用会员等级-独秀",
                "growth": 99999,
                "effectiveType": "CYCLE",
                "effectiveDate": 1,
                "thirdLevelConnectKey": "1",
                "levelIcon": "https://buorgcostest.rongyi.com/member-logo/258/156804920652208771845907650977469920977.jpeg",
                "id": level_id
            }
            member.info(f"编辑等级的请求参数：{data}")
            return requests.patch(url, headers=header, json=data)

    # 删除会员等级
    def delete_member_leve(self, env, level_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/member/level/{}".format(level_id)
        member.info(f"删除等级的id：{level_id}")
        return requests.delete(url, headers=header)

    # 等级列表查询
    def search_member_level_list(self, env, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/member/level"
        return requests.get(url, headers=header)

    # 获取隐私配置信息
    # 获取静默授权信息
    def silent_authorization_configuration(self, env, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/memberConfig/"
        return requests.get(url, headers=header)

    # 新增隐私配置
    def add_privacy_config(self, env, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/memberConfig"
        data = {"privacyGreedy": True, "fansAsMember": False, "snsapiBase": True}
        member.info(f"新增隐私配置的请求参数：{data}")
        return requests.post(url, headers=header, json=data, )

    # 修改隐私配置
    def edit_privacy_config(self, env, privacy_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/memberConfig/" + str(privacy_id)
        data = {"privacyGreedy": False, "fansAsMember": False, "snsapiBase": False, "id": privacy_id}
        member.info(f"修改隐私配置的请求参数：{data}")
        return requests.patch(url, headers=header, json=data)

    # 基本信息
    def get_member_info(self, env, member_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/memberLabel/info?memberId={}".format(member_id)
        member.info(f"查询会员基本信息的id：{member_id}")
        return requests.get(url, headers=header)

    # 根据会员编号查询
    def get_member_info_by_card_code(self, env, card_code, branch_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "openapi-service/openapi/v1/member/getInfoByCardCode"
        member.info(f"会员编号查询请求参数：{card_code},{branch_id}")
        return requests.get(url, params={"cardCode": card_code, "branchId": branch_id}, headers=header)

    # 修改会员
    def edit_member_info(self, env, branch_id, data, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "openapi-service/openapi/v1/member/updateBaseInfoByIdOrCardCode"
        member.info(f"修改会员的请求参数：{data},组织节点id：{branch_id}")
        return requests.post(url, json=data, params={"branchId": branch_id}, headers=header)

    # 会员注册
    def register_member_info(self, env, data, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "openapi-service/openapi/v1/member/create"
        member.info(f"会员注册的请求参数：{data}")
        return requests.post(url, json=data, headers=header)

    # 合并
    def member_merge(self, env, member_card_code_owner, member_card_code_subset, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/member/merge"
        data = {"memberCardCodeOwner": member_card_code_owner, "memberCardCodeSubset": member_card_code_subset}
        member.info(f"会员合并的请求参数：{data}")
        return requests.post(url, json=data, headers=header)

    # 编辑和新增标签都是这个接口
    def create_label(self, env, value, label_group_id, introduce, header, label_id=None):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/label/edit"
        if not label_id:
            data = {"value": value, "groupId": label_group_id, "introduce": introduce}
            member.info(f"创建标签的请求参数：{data}")
            return requests.post(url, json=data, headers=header)
        else:
            data = {
                "value": value,
                "groupId": label_group_id,
                "introduce": introduce,
                "id": label_id
            }
            member.info(f"修改标签的请求参数：{data}")
            return requests.post(url, headers=header, json=data)

    # 删除标签
    def delete_label(self, env, label_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/label/delete"
        data = {
            "ids": [
                f"{label_id}"
            ]
        }
        member.info(f"删除标签的id：{label_id}")
        return requests.post(url, headers=header, json=data)

    # 标签列表
    def label_list(self, env, data, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/label/list"
        member.info(f"查询标签列表的请求参数：{data}")
        return requests.post(url, headers=header, json=data)

    # 会员详情会员打标签
    def member_label(self, env, member_id, label_list, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/memberLabel/edit"
        data = {"memberId": member_id, "labelType": 1, "labelList": label_list}  # labelType，1必然标签 2.可能性标签
        member.info(f"会员绑定标签的请求参数：{data}")
        return requests.post(url, headers=header, json=data)

    # 单个标签查询
    def get_label(self, env, label_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"platform-member-server/v1/label/{label_id}"
        member.info(f"查询标签id：{label_id}")
        return requests.get(url, headers=header)

    # 更改标签关联对象
    def label_object(self, env, object_ids, object_id, label_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"platform-member-server/v1/labelObject/{object_ids}"
        if object_id is None or label_id is None:
            data = []
        else:
            data = [{"objectId": object_id, "labelId": label_id}]
        member.info(f"更改标签关联对象的请求参数：{data}")
        return requests.patch(url, headers=header, json=data)

    # 标签组查询
    def label_group_list(self, env, num1, num2, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"platform-member-server/v1/labelGroup?size={num1}&current={num2}"
        member.info(f"标签组查询的数量：{num1},页码：{num2}")
        return requests.get(url, headers=header)

    # 创建标签组
    def create_label_group(self, env, group_name, group_desc, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "platform-member-server/v1/labelGroup"
        data = {"groupName": group_name, "groupDesc": group_desc}
        member.info(f"创建标签组的请求参数：{data}")
        return requests.post(url, headers=header, json=data)

    # 编辑标签组：
    def edit_label_group(self, env, group_id, group_name, group_desc, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"platform-member-server/v1/labelGroup/{group_id}"
        data = {"groupName": group_name, "groupDesc": group_desc}
        member.info(f"修改标签组的id：{group_id}和请求参数：{data}")
        return requests.patch(url, headers=header, json=data)

    # 删除标签组：
    def delete_label_group(self, env, group_id, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + f"platform-member-server/v1/labelGroup/{group_id}"
        member.info(f"删除的标签组id：{group_id}")
        return requests.delete(url, headers=header)

    # 绑定
    def bind_operate(self, env, member_id, card_code, identifier_ns, identifier, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "openapi-service/openapi/v1/member/bindIdentifierByIdOrCardCode"
        if identifier_ns == None or "":
            type = "PHONE"
        else:
            type = "OFFICIAL_ACCOUNTS"
        data = {"id": member_id, "cardCode": card_code, "identifierNs": identifier_ns, "identifier": identifier,
                "type": type}
        member.info(f"会员绑定的请求参数：{data}")
        return requests.post(url, headers=header, json=data)

    # 解绑
    def unbind_operate(self, env, member_id, card_code, identifier_ns, identifier, type, header):
        base_url = getconf(env, "zhongtai_url")
        url = base_url + "openapi-service/openapi/v1/member/unbindIdentifierByIdOrCardCode"
        data = {"id": member_id, "cardCode": card_code, "identifierNs": identifier_ns, "identifier": identifier,
                "type": type}
        member.info(f"会员解绑的请求参数：{data}")
        return requests.post(url, headers=header, json=data)
